package com.lwj.test.security;


import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.security.SecureRandom;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
public class CGenerator{
	   byte rawKeyData[]=null;
	   public void CGenCredit(String filename){
		   SecretKey key=null;
		   SecureRandom sr=null;
		   KeyGenerator kg=null;
           File file=null;
           DataOutputStream out=null;
		   try{
		   	 System.out.print("Gen.");
		     sr = new SecureRandom();
		     kg = KeyGenerator.getInstance("DES");
		     kg.init(sr);
		     key = kg.generateKey();
		     rawKeyData = key.getEncoded();
             file = new File(filename);
             out = new DataOutputStream(new FileOutputStream(file));
             out.write(rawKeyData);
             out.close();
             System.out.println("!");
		   }
		   catch(Exception ex){
			 ex.printStackTrace();
		   }
	   }
	   public void ENCode(String filePath){
		   byte rawKeyData[]=null;
		   SecretKey key=null;
		   SecureRandom sr=null;
		   DESKeySpec dks=null;
		   SecretKeyFactory keyFactory=null;
		   Cipher cipher=null;
		   byte data[]=null;
		   byte[] encryptedData=null;
		   File file=null;
		   DataOutputStream out=null;
		   File files[]=null;
		   int i;
		   String newPath=filePath+File.separator+"new";
		   File dir=null;
		   String filename="";
		   File newFile=null;
		   try{
		   	   System.out.print("ENcode.");
			   sr = new SecureRandom();
			   dks = new DESKeySpec(this.rawKeyData);
			   keyFactory = SecretKeyFactory.getInstance("DES");
			   key = keyFactory.generateSecret(dks);
			   cipher = Cipher.getInstance("DES");
			   cipher.init(Cipher.ENCRYPT_MODE,key,sr );
			   files=this.getFiles(filePath);
			   if(files.length>0){
				 dir=new File(newPath);
				 if (!dir.exists())
					 dir.mkdir();
			   }
			   for(i=0;i<files.length;i++){
			      file=files[i];
			      filename=file.getName();
			      System.out.print(".");
			      data=this.getFileByte(file);
			      encryptedData =cipher.doFinal(data);
			      newFile=new File(dir.getAbsolutePath()+File.separator+filename);
		          out = new DataOutputStream(new FileOutputStream(newFile));
		          out.write(encryptedData);
		          out.close();
		       }
		       System.out.println("!");
		    }
		    catch(Exception ex){
			  ex.printStackTrace();
		    }
	}
	public void DECode(String keyfilename,String filePath){
		   byte rawKeyData[]=null;
		   SecretKey key=null;
		   SecureRandom sr=null;
		   DESKeySpec dks=null;
		   SecretKeyFactory keyFactory=null;
		   Cipher cipher=null;
		   byte data[]=null;
		   byte[] encryptedData=null;
		   File file=null;
		   DataOutputStream out=null;
		   File files[]=null;
		   int i;
		   String newPath=filePath+File.separator+"new";
		   File dir=null;
		   String filename="";
		   File newFile=null;
		   try {
			   rawKeyData=this.getFileByte(new File(keyfilename));
			   sr = new SecureRandom();
			   dks = new DESKeySpec(this.rawKeyData);
			   keyFactory = SecretKeyFactory.getInstance("DES");
			   key = keyFactory.generateSecret(dks);
			   cipher = Cipher.getInstance("DES");
			   cipher.init(Cipher.DECRYPT_MODE,key,sr );
			   files=this.getFiles(filePath);
			   for(i=0;i<files.length;i++){
			      file=files[i];
			      filename=file.getName();
			      System.out.print(file.getAbsolutePath()+"..");
			      data=this.getFileByte(file);
			      encryptedData =cipher.doFinal(data);
		          System.out.println("!");
		       }
		   }
		   catch (Exception ex) {
		   }
		  
	}
	public File[] getFiles(String filePath){
		   File fileDir = new File(filePath);
		   File files[]=null;
		   FileFilter fileter=null;
		   try{
             if (fileDir.exists()) {
                fileter=new FileFilter();
                files = fileDir.listFiles(fileter);
             }
           }catch(Exception ex){
        	 ex.printStackTrace(); 
           } 
           return files;
	}
	public static void main(String[] args) {
		   String enPath=args[0];
		   String creditFilename=enPath+File.separator+"cry.class";
		   try{
			String filePath=enPath;
			CGenerator app=new CGenerator();
			app.CGenCredit(creditFilename);
			app.ENCode(filePath);
	       }
	       catch(Exception ex){
		     ex.printStackTrace();
	       }
	}
	   public byte[] getFileByte(File file){
	   	      byte[] data=null;
	   	      int filesize=0;
              try {
                  filesize = (int) file.length();
                  data = new byte[filesize];
                  DataInputStream in = new DataInputStream(new FileInputStream(file));
                  in.readFully(data);
                  in.close();
              }
              catch (Exception ex) {
                
              }
              return data;
	   }
	   class FileFilter implements java.io.FileFilter
	   {
	               public boolean accept(File file)
	               {
	                   if(file.getName().indexOf(".class")!=-1 || file.isDirectory())
	                     return true;
	                   return false;
	               }
	               public String getDescription()
	               {
	                   return "(*.class) class File";
	               }
	   }	   
}
